package com.godz.frontData.index3;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.godz.mapper.eduPrimMapper;
import com.godz.mapper.index3EduExpMapper;
import com.godz.pojo.eduPrim;
import com.godz.pojo.index3EduExp;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
public class eduPrimData {
    final
    index3EduExpMapper eduMapper;
    final
    eduPrimMapper primMapper;

    public eduPrimData(index3EduExpMapper eduMapper, eduPrimMapper primMapper) {
        this.eduMapper = eduMapper;
        this.primMapper = primMapper;
    }

    /**
     * 前半部分全是教育列表
     */
    @GetMapping("/index3/get/eduExp")
    public List<index3EduExp> getEduExp() {
        return eduMapper.queryAllEduExp();
    }

    //查询单个
    @GetMapping("/index3/get/eduExpOne/{intId}")
    public index3EduExp getOneEdu(@PathVariable("intId") Integer integer) {
        return eduMapper.selectOne(
                new QueryWrapper<index3EduExp>().eq("id", integer)
        );
    }

    //修改一条
    @PostMapping("/index/post/editEduExp")
    public String editEduExp(@RequestBody index3EduExp eduExp) {
        QueryWrapper<index3EduExp> wrapper = new QueryWrapper<>();
        wrapper.eq("id", eduExp.getId());

        index3EduExp eduExp1 = new index3EduExp();

        eduExp1.setEeSeva(eduExp.getEeSeva());
        eduExp1.setEeEra(eduExp.getEeEra());
        eduExp1.setEeOve(eduExp.getEeOve());
        eduExp1.setEeStart(eduExp.getEeStart());
        eduExp1.setEeEnd(eduExp.getEeEnd());
        eduExp1.setEeSname(eduExp.getEeSname());

        eduMapper.update(eduExp1, wrapper);
        return "Update OK";
    }

    /***
     * 后半部分教育发生内容故事
     */
    @GetMapping("/index3/get/eduPrim")
    public List<eduPrim> getEduPrim(@RequestParam(value = "deadNow", defaultValue = "0") Integer deadNow) {
        if (deadNow==0){
            return primMapper.queryAllEduPrim();
        }else{
            QueryWrapper<eduPrim> wrapper = new QueryWrapper<eduPrim>()
                    .like("ee_linkid", deadNow);
            return primMapper.selectList(wrapper);
        }
    }
    //查询单个
    @GetMapping("/index3/get/eduPrimOne/{intId}")
    public eduPrim getOnePrim(@PathVariable("intId") Integer integer) {
        return primMapper.selectOne(
                new QueryWrapper<eduPrim>().eq("id", integer)
        );
    }

    //增加一条
    @PostMapping("/index3/post/addPrim")
    public String addPrim(@RequestBody eduPrim prim) {
        primMapper.insert(prim);
        return "insert OK";
    }

    //修改一条
    @PostMapping("/index3/post/editPrim")
    public String editPrim(@RequestBody eduPrim prim) {
        QueryWrapper<eduPrim> wrapper = new QueryWrapper<>();
        wrapper.eq("id", prim.getId());

        eduPrim prim1 = new eduPrim();

        prim1.setEeLinkid(prim.getEeLinkid());
        prim1.setPrimTitle(prim.getPrimTitle());
        prim1.setPrimSeva(prim.getPrimSeva());
        prim1.setPrimCon(prim.getPrimCon());
        prim1.setPrimStart(prim.getPrimStart());
        prim1.setPrimEnd(prim.getPrimEnd());
        prim1.setPrimImg(prim.getPrimImg());

        primMapper.update(prim1, wrapper);
        return "Update OK";
    }

    //删除一条打字机
    @GetMapping("/index3/get/deletePrim/{id}")
    public String deletePrim(@PathVariable("id") Integer integer) {
        primMapper.deleteById(integer);
        return "Delete Ok";
    }

    //根据id和标题信息查询
    @GetMapping("/queryPrim/{title}")
    public List<eduPrim> queryPrimByTitle(@PathVariable("title") String title) {
        QueryWrapper<eduPrim> wrapper = new QueryWrapper<eduPrim>()
                .like("prim_title", title)
                .or()
                .eq("id", title);
        return primMapper.selectList(wrapper);
    }

}
